home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / libscs / hdhandle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  10.7 KB  |  401 lines

  1. /*    HeaDer_HANDLE. C
  2. #
  3. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4.  
  5. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  6. Permission is granted to reproduce this software for non-commercial
  7. purposes provided that this notice is left intact.
  8.  
  9. It is acknowledged that the U.S. Government has rights to this software
  10. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  11. and the University of California.
  12.  
  13. This software is provided as a professional and academic contribution
  14. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  15. with no warranties of any kind whatsoever, no support, no promise of
  16. updates, or printed documentation. By using this software, you
  17. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  18. University of California shall have no liability with respect to the
  19. infringement of other copyrights by any part of this software.
  20.  
  21. For further information about this notice, contact William Johnston,
  22. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  23. (wejohnston@lbl.gov)
  24.  
  25. For further information about this software, contact:
  26.         Jin Guojun
  27.         Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  28.         g_jin@lbl.gov
  29.  
  30. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  31. %
  32. % AUTHOR:    Jin Guojun - LBL    7/25/91
  33. */
  34.  
  35. #ifdef    COMMON_TOOL
  36.  
  37. #ifndef    MaxColors
  38. #define MaxColors 256
  39. #endif
  40.  
  41. ICC_HEADER    icchd;
  42.  
  43. icc_header_handle(job, img, ac, av, assist)
  44. U_IMAGE    *img;
  45. int    ac;
  46. char    **av;
  47. VType    *assist;
  48. {
  49. int    num;
  50.  
  51. switch (job) {
  52. case HEADER_READ:
  53. case HEADER_FREAD:
  54.     if (!av)
  55.         fseek(img->IN_FP, 0, 0);
  56.     fread(&num, sizeof(num), 1, img->IN_FP);
  57.     if (num != ICC_MAGIC)
  58. other_hd:    return    EOF;
  59.     fread(&icchd.hd_len, sizeof(icchd.hd_len), 1, img->IN_FP);
  60.     fread(&icchd.H_W, icchd.hd_len - sizeof(icchd.hd_len), 1, img->IN_FP);
  61.     fread(&icchd.img_hd.len, sizeof(icchd.img_hd.len), 1, img->IN_FP);
  62.     fread(&icchd.img_hd.length, icchd.img_hd.len-sizeof(icchd.img_hd.len),
  63.         1, img->IN_FP);
  64.     if (icchd.img_hd.length - icchd.img_hd.len -
  65.         icchd.img_hd.x_size * icchd.img_hd.y_size)
  66.         goto    other_hd;
  67.     img->width = icchd.img_hd.orig_width;
  68.     img->height = icchd.img_hd.orig_height;
  69.     if (!(img->width | img->height) || img->height != icchd.img_hd.y_size
  70.         || ((img->width + 7) & 0xfffffff8L != icchd.img_hd.x_size))
  71.         img->width = icchd.img_hd.x_size,
  72.         img->height = icchd.img_hd.y_size;
  73.     img->pxl_in = img->frames = 1;
  74.     img->channels = img->dpy_channels = icchd.img_hd.planes;
  75.     img->in_type = ICC;
  76.     img->in_color = img->color_form = CFM_SGF;
  77.     if (img->channels==3)
  78.         img->in_form = IFMT_SEPLANE,
  79.         img->in_color = CFM_SEPLANE;
  80.     else    img->in_form = IFMT_SGF,
  81.         img->mono_img = True;
  82.     break;
  83. default:    message("unknown icc job %d\n", job);
  84.     return    job;
  85. }
  86. return    0;
  87. }
  88.  
  89.  
  90. static    int    format, maxval;
  91. static    xel    **xel24;
  92. static colorhash_table    cht;
  93.  
  94.  
  95. static    cmap_t*
  96. pnmmap_to_regmap(rg_cmap, chv, colors)
  97. cmap_t    *rg_cmap[3];
  98. colorhist_vector chv;
  99. int    colors;
  100. {
  101. register int    i=3, j=1<<min_bits(colors-1);
  102. register cmap_t    *cmap[3];
  103.  
  104. verify_buffer_size(rg_cmap, j, i*sizeof(*cmap), "pnm-rcmap");
  105. cmap[0] = rg_cmap[0];
  106. cmap[1] = rg_cmap[1] = rg_cmap[0] + j;
  107. cmap[2] = rg_cmap[2] = rg_cmap[1] + j;
  108.  
  109. for (i=0; i < colors; ++i) {
  110.     cmap[0][i] = PPM_GETR(chv[i].color);
  111.     cmap[1][i] = PPM_GETG(chv[i].color);
  112.     cmap[2][i] = PPM_GETB(chv[i].color);
  113.     }
  114. return    cmap[0];
  115. }
  116.  
  117. pnm_header_handle(job, img, ac, av, assist)
  118. U_IMAGE    *img;
  119. int    ac;
  120. char    *av[];
  121. VType    *assist;
  122. {
  123. gray    gmaxval;    /* byte */
  124. colorhist_vector    chv;
  125. xel    *xelrow, p;
  126. int    depth, colors;
  127. register int    r;
  128.  
  129.     switch (job) {
  130.     case HEADER_READ:
  131.     case HEADER_FREAD:
  132.     if (img->in_type > RLE && img->in_type != PNM)    goto    pnm_df;
  133.     if (!av)
  134.         fseek(img->IN_FP, 0, 0);
  135.     format = pbm_readmagicnumber(img->IN_FP);    /* check magic # */
  136.     img->pxl_in = 1;
  137.     switch (PNM_FORMAT_TYPE(format)) {
  138.     case PPM_TYPE:
  139.     maxval = ppm_readppminitrest(img->IN_FP, &img->width, &img->height);
  140.     xel24 = (xel**)alloc_2d_discrete(img->width, img->height, sizeof(xel));
  141.         for (r=0; r < img->height; r++)
  142.             pnm_readpnmrow(img->IN_FP, xel24[r], img->width, maxval, format);
  143.         /* Figure out the proper depth and colormap */
  144.         chv = ppm_computecolorhist(xel24, img->width, img->height,
  145.                 MaxColors, &colors);
  146.         if (chv) {
  147.             DEBUGMESSAGE("%d colors found", colors);
  148.             if (maxval != 255)
  149.                 for (r=0; r < colors; r++)
  150.                 PPM_DEPTH(chv[r].color, chv[r].color, maxval, 255);
  151.  
  152.         /* Force white to slot 0 and black to slot 1, if possible. */
  153.             PPM_ASSIGN(p, 255, 255, 255);
  154.             ppm_addtocolorhist(chv, &colors, MaxColors, &p, 0, 0);
  155.             PPM_ASSIGN(p, 0, 0, 0);
  156.             ppm_addtocolorhist(chv, &colors, MaxColors, &p, 0, 1);
  157.  
  158.             if (colors != 2) {
  159.             /* convert the ppm colormap into the RLE colormap. */
  160.                 depth = 8;
  161.                 img->cmaplen = colors;
  162.                 img->in_color = CFM_SCF;
  163.                 img->in_form = IFMT_SCF;
  164.                 pnmmap_to_regmap(reg_cmap, chv, colors);
  165.             }
  166.             if (cht)    ppm_freecolorhash(cht);
  167.             cht = ppm_colorhisttocolorhash(chv, colors);
  168.             ppm_freecolorhist(chv);
  169.             img->channels = img->mono_img = 1;
  170.             img->in_type = PNM;
  171.             if (colors==2)    {    /* Monochrome. */
  172.                 depth = 1;
  173.                 goto    pbm_tp;
  174.             }
  175.         }
  176.         else    img->channels = 3,
  177.             img->in_type = PPM,
  178.             img->in_form = IFMT_ILC,
  179.             img->in_color = CFM_ILC;
  180.     break;
  181.     case PGM_TYPE:
  182.         pgm_readpgminitrest(img->IN_FP, &img->width, &img->height, &gmaxval);
  183.         maxval = (xelval) gmaxval;
  184.         if (maxval > 255)
  185. #    ifdef    BIGGRAYS
  186.             img->in_form = IFMT_SHORT,
  187.             img->pxl_in = sizeof(short);
  188. #    else
  189.         return    prgmerr(0, "maxval (%d) is greater than 255", maxval);
  190. #    endif
  191.         img->channels = img->mono_img = 1;
  192.         img->in_type = PGM;
  193.         img->in_color = CFM_SGF;
  194.     break;
  195.     case PBM_TYPE:
  196.         pbm_readpbminitrest(img->IN_FP, &img->width, &img->height);
  197.         maxval = img->channels = img->mono_img = 1;
  198.         img->in_type = PBM;
  199. pbm_tp:        img->in_form = IFMT_BITMAP;
  200.         img->in_color = CFM_BITMAP;
  201.     break;
  202.     default:
  203. pnm_df:        return    EOF;
  204.     }
  205.     if (maxval > 255 && depth != 1)
  206.         message("maxval is not 255 - automatically rescaling colors");
  207.  
  208.     break;
  209.     case HEADER_WRITE:
  210.     default:    return    prgmerr(0, "not a pnm job %d\n", job);
  211.     }
  212. return    0;
  213. }
  214.  
  215.  
  216. static    bool    TiffRGB;
  217. TIFF    *TIFFin;
  218. unsigned short    samplesperpixel, bitspersample;
  219.  
  220. tiff_header_handle(job, img, ac, av, assist)
  221. U_IMAGE    *img;
  222. int    ac;
  223. char    **av;
  224. VType    *assist;
  225. {
  226. unsigned short    maxval;
  227.  
  228. switch (job) {
  229. case HEADER_READ:
  230. case HEADER_FREAD:
  231.     if (!av)
  232.         lseek(fileno(img->IN_FP), 0, 0);
  233.     TIFFin = TIFFFdOpen(fileno(img->IN_FP), img->name ? img->name:"", "r");
  234.     if (!TIFFin)
  235.         return    EOF;
  236.     TIFFGetField(TIFFin, TIFFTAG_BITSPERSAMPLE, &bitspersample);
  237.     TIFFGetField(TIFFin, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  238.     if (samplesperpixel > 1)
  239.         TiffRGB++;
  240.     switch (samplesperpixel) {
  241.     default:message("handle %d samples.\n", samplesperpixel);
  242.     case 1:
  243.         img->channels = 1;
  244.         if (img->color_dpy && !assist)
  245.             img->dpy_channels = 3;
  246.         else    img->dpy_channels = img->mono_img = 1;
  247.         if (TIFFin->tif_dir.td_photometric==PHOTOMETRIC_PALETTE) {
  248.         register int    i = MIN(MaxColors, (maxval=1<<bitspersample));
  249.  
  250.             img->in_color = CFM_SCF;
  251.             img->in_form = IFMT_SCF;
  252.             img->cmaplen = i;
  253.             if (verify_buffer_size(reg_cmap, i, 3*sizeof(cmap_t), "tif_cmap")){
  254.             reg_cmap[1] = reg_cmap[0] + i;
  255.             reg_cmap[2] = reg_cmap[1] + i;
  256.             }
  257.             while (i--) {
  258.             reg_cmap[0][i] = TIFFin->tif_dir.td_colormap[0][i] >> 8;
  259.             reg_cmap[1][i] = TIFFin->tif_dir.td_colormap[1][i] >> 8;
  260.             reg_cmap[2][i] = TIFFin->tif_dir.td_colormap[2][i] >> 8;
  261.             }
  262.         }
  263.         else if (bitspersample==1)    /* always convert to SGF */
  264.             img->in_color = CFM_SGF,
  265.             img->in_form = IFMT_BITMAP;
  266.         else    prgmerr(0, "TIFF format-8");
  267.         break;
  268.     case 3:
  269.         if (TIFFin->tif_dir.td_planarconfig==PLANARCONFIG_SEPARATE)
  270.             img->in_color = CFM_ILL,
  271.             img->in_form = IFMT_ILL;
  272.         else    img->in_color = CFM_ILC,
  273.             img->in_form = IFMT_ILC;
  274.         goto    next34;
  275.     case 4:    img->in_color = CFM_ALPHA;
  276.         img->in_form = IFMT_ALPHA;
  277. next34:        img->dpy_channels = img->channels = 3;
  278.         if (!img->color_dpy)
  279.             img->dpy_channels = 1;
  280.         break;
  281.     }
  282.  
  283.     TIFFGetField(TIFFin, TIFFTAG_IMAGEWIDTH, &img->width);
  284.     TIFFGetField(TIFFin, TIFFTAG_IMAGELENGTH, &img->height);
  285.  
  286.     img->in_type = TiFF;
  287.     img->pxl_in = sizeof(byte);
  288.     break;
  289. case HEADER_WRITE:
  290. default:    return    prgmerr(0, "no such a tiff job %d\n", job);
  291. }
  292. return    0;
  293. }
  294.  
  295. #endif    COMMON_TOOL
  296.  
  297. int    row_dpy_mode;
  298.  
  299. ccs_get_row_ok(img)
  300. register U_IMAGE*    img;
  301. {
  302. register int    type=img->in_type, r_mode=row_dpy_mode && img->IN_FP==stdin;
  303.  
  304. if (type==RLE && r_mode)    return    -1;    /* decreasing mode    */
  305.  
  306. r_mode |= type==HIPS && img->color_form==CFM_SGF;
  307.  
  308. #ifdef    STREAM_IMAGE
  309.     if ((r_mode &= !get_sinfo(True, 0)) &&    /* not in compressed type */
  310.         (type=get_sinfo(False, 0)) > 0)    /* is in stream mode.    */
  311.         for (type*=img->width; type--;)
  312.             fgetc(img->IN_FP);
  313. #endif
  314. return    r_mode;
  315. }
  316.  
  317. ccs_get_row(img, row, no_buf)
  318. U_IMAGE    *img;
  319. register int    row;
  320. {
  321. register int    w = img->width;
  322. register char*    buf = (char*)img->src + (no_buf ? 0 : w * row * img->channels);
  323. switch (img->in_type)    {
  324. case HIPS:
  325.     return    fread(buf, 1, w, img->IN_FP);
  326. #ifdef    RLE_IMAGE
  327. case RLE:    {
  328.     char*    scan[3];
  329.     scan[0] = buf;    scan[1] = buf + w;    scan[2] = buf + (w << 1);
  330.     rle_getrow(&rle_dflt_hdr, scan);
  331.     return    w;
  332.     }
  333. #endif
  334. default:
  335.     return    prgmerr(0, "cannot get_row for %s", ITypeName[img->in_type]);
  336. }
  337. }
  338.  
  339. #ifdef    HIPS_IMAGE
  340.  
  341. #define    si_elems    "si_elems"
  342. #define si_draws    "si_draws"
  343. #define    si_texts    "si_texts"
  344. #define    si_str    "sit%03d"
  345.  
  346. static    char    sbuf[8];
  347.  
  348. put_superimpose_param(img, hhd)
  349. U_IMAGE*    img;
  350. struct header*    hhd;
  351. {
  352. if (img->superimpose || img->draws | img->texts)    {
  353. register superimpose_elems*    si_ep=img->superimpose[1];
  354.     if (findparam(hhd, si_elems) != NULLPAR)
  355.         hhd->paramdealloc = False;
  356.     setparam(hhd, si_elems, PFSHORT, 2, &img->draws);
  357.     if (img->draws)
  358.         setparam(hhd, si_draws, PFBYTE, img->draws * sizeof(*si_ep),
  359.             img->superimpose[0]);
  360.     if (img->texts)    {
  361.     register int    i=img->texts;
  362.         setparam(hhd, si_texts, PFBYTE, img->texts * sizeof(*si_ep),
  363.             si_ep);
  364.         while (i--)    {
  365.         sprintf(sbuf, si_str, i);
  366.         setparam(hhd, sbuf, PFBYTE, strlen(si_ep[i].elem.text.content),
  367.         si_ep[i].elem.text.content);
  368.         }
  369.     }
  370. }
  371. }
  372.  
  373. get_superimpose_param(img, hhd)
  374. U_IMAGE*    img;
  375. struct header*    hhd;
  376. {
  377. int    n=2;
  378. short    *boo;
  379. if (!findparam(hhd, si_elems))    return    False;
  380. img->superimpose = zalloc(n, sizeof(superimpose_elems), "");
  381. if (getparam(hhd, si_elems, PFSHORT, &n, &boo) != HIPS_OK)
  382.     return    EOF;
  383. img->draws = boo[0];    n = 2;
  384. if ((findparam(hhd, si_draws) != NULLPAR | n) &&
  385.     getparam(hhd, si_draws, PFBYTE, &n, img->superimpose) != HIPS_OK)
  386.     img->draws = 0;
  387. img->texts = boo[1];    n = 2;
  388. if (findparam(hhd, si_texts) != NULLPAR  &&
  389.     getparam(hhd, si_texts, PFBYTE, &n, img->superimpose+1) == HIPS_OK)    {
  390.     register superimpose_elems*    si_ep=img->superimpose[1];
  391.     register int    i=img->texts;
  392.     while (i--)    {
  393.     sprintf(sbuf, si_str, i);    n = 2;    /* non-single char    */
  394.     getparam(hhd, sbuf, PFBYTE, &n, &si_ep[i].elem.text.content);
  395.     }
  396. } else    img->texts = 0;
  397. return    img->texts | img->draws;
  398. }
  399.  
  400. #endif
  401.